//
//  BubbleView.m
//  ForengnMenu
//
//  Created by @-@ on 16/1/19.
//  Copyright © 2016年 余旺. All rights reserved.
//

#import "BubbleView.h"

@implementation BubbleView

-(id)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    
    if (self) {
        
        self.contentEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 15);
        
        
        [self.contentLabel addObserver:self forKeyPath:@"text" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
        
    }
    
    return self;
}


#pragma mark  - get set
-(UIImageView * )bubbleImageView{
    
    if (!_bubbleImageView) {//_bubbleImageView == nil;
        
        _bubbleImageView = [UIImageView new];
        _bubbleImageView.frame = self.bounds;
        
        //插入最底层
        [self insertSubview:_bubbleImageView atIndex:0];
        
    }
    
    return _bubbleImageView;
    
}

-(UILabel * )contentLabel{
    
    if (_contentLabel == nil) {//!_contentLabel
        
        _contentLabel = [UILabel new];
        
        CGRect rect = self.bounds;
        rect.origin.x = self.contentEdgeInsets.left;
        rect.origin.y = self.contentEdgeInsets.top;
        
        rect.size.width = rect.size.width - self.contentEdgeInsets.left - self.contentEdgeInsets.right;
        rect.size.height = rect.size.height - self.contentEdgeInsets.top - self.contentEdgeInsets.bottom;
        
        _contentLabel.frame = rect;
        
        //        _contentLabel.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.5];
        
        _contentLabel.numberOfLines = 0;//文本行数
        
        [self addSubview:_contentLabel];
        
    }
    
    return _contentLabel;
}


#pragma mark  - KVO

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
    
    NSString * text = change[@"new"];
    
    //通过文本信息计算尺寸大小
    CGFloat maxWidth = self.bounds.size.width - self.contentEdgeInsets.left - self.contentEdgeInsets.right;
    CGSize maxSize = CGSizeMake(maxWidth, MAXFLOAT);
    
    //计算尺寸的属性信息
    NSDictionary * dic = @{NSFontAttributeName: self.contentLabel.font};
    
    CGRect rect = [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    //拿到label大小
    CGRect labelRect = self.contentLabel.frame;
    labelRect.size.width = rect.size.width;
    labelRect.size.height = rect.size.height;
    
    self.contentLabel.frame = labelRect;
    
    CGRect imageRect = self.bubbleImageView.frame;
    
    imageRect.size.height = rect.size.height + self.contentEdgeInsets.bottom + self.contentEdgeInsets.top;
    
    imageRect.size.width = rect.size.width + self.contentEdgeInsets.left + self.contentEdgeInsets.right;
    
    self.bubbleImageView.frame = imageRect;
    
    
    NSLog(@"%@",NSStringFromCGRect(rect));
    
}

-(void)dealloc{
    
    //释放键值观察者
    [self.contentLabel removeObserver:self forKeyPath:@"text"];
    
}


@end
